<?php

namespace Modules\Admin\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;
use Modules\Admin\Entities\Configuration;
use Modules\Admin\Traits\AuthenticatesUsers;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    public $redirectTo;

    public function __construct()
    {
        $this->redirectTo = route('admin.index');
    }


    public function index()
    {
        return view('admin::login');
    }


    public function showLoginForm(){
        return view('admin::login');
    }


    /**
     * @return \Illuminate\Http\RedirectResponse
     */
    public function sendFailedLoginResponse(){
        return redirect()->back()->withErrors('用户名或密码错误');
    }



    protected function guard()
    {
        return Auth::guard('admin');
    }



    public function username()
    {
        return 'username';
    }



    protected function authenticated(Request $request, $user)
    {
        //缓存配置信息
        $configuration = Configuration::query()->pluck('val', 'key');
        $request->session()->put('configuration', $configuration);
    }


    /**
     * 退出后的动作
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    protected function loggedOut(Request $request)
    {
        return Redirect::to(URL::route('admin.login'));
    }
}
